// 验证是否当前窗口为最顶端，还是在iframe中
function CkeckTopWindow() {
    let result = 1;
    if (parent.top.location.href !== window.location.href) {
        parent.top.location.replace(window.location.href + '&action=expired');
    } else {
        result = 0;
    }
    return result;
}

function GetSignupData() {
    let data = new Object();
    data.username = $.trim($("#username").val());
    data.password = $.trim($("#password").val());
    data.rememberMe = false;//$("#remember").is(":checked");

    return JSON.stringify(data);
}

function DoSignUp($self) {
    //step 1、进行页面验证，失败则返回
    if (!$("#form__login").valid())
        return false;

    //step 2、执行登录处理
    $.ajax({
        type: "POST",
        url: "doAjaxSingup.html",
        timeout: 60 * 1000,
        data: GetSignupData(),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
            $self.attr("disabled", "disabled");
            $self.text("Login...");
            $self.removeClass("button_hover");
            $self.addClass("waiting");
        }
    }).done(function (data) {
        if (data.Success === true) {
            // 成功处理，跳转到默认定向URL
            document.location.replace('dashBoard.html');
        } else {
            setTimeout(function () {
                $self.removeAttr("disabled");
                $self.text("登录");
                $self.removeClass("waiting");
                $self.addClass("button_hover");
                $(".note").text(data.Message);
                $(".note").removeClass("note__down").addClass("note__up");
            }, 500);
        }
    }).fail(function (data) {
        let errShowMsg = "";
        if (data.Message !== undefined && data.Message !== null) {
            errShowMsg = data.Message;
        } else {
            if (data.status === 0) {
                errShowMsg = "错误：服务器无响应，请检查网络连接。";
            } else if (data.status === 401) {
                errShowMsg = "时错误：未经授权,访问被系统拒绝。";
            } else if (data.status === 404) {
                errShowMsg = "错误：服务器找不到请求的资源。";
            } else if (data.status === 500) {
                errShowMsg = "错误：发生未知错误，请联系管理员。";
            }
        }
        setTimeout(function () {
            $self.removeAttr("disabled");
            $self.text("登录");
            $self.removeClass("waiting");
            $self.addClass("button_hover");
            $(".note").text(errShowMsg);
            $(".note").removeClass("note__down").addClass("note__up");
        }, 500);
    }).always(function () {
    });
}

function OnInputFocus($self) {
    $(".note").text("");
    $(".note").removeClass("note__up").addClass("note__down");

    if ($self.attr("name") === "_username") {
        $self.removeClass("error");
        $("#username-error").parent().remove();
    }
    if ($self.attr("name") === "_password") {
        $self.removeClass("error");
        $("#password-error").parent().remove();
    }
}
/*------------------------------------------------------------------------------
 画面初始化
 ------------------------------------------------------------------------------*/
$(document).ready(function () {
});

/*
 -------------------------------------------------------------------------------
 系统验证函数
 -------------------------------------------------------------------------------
 */
$(function () {
    $("#form__login").validate({
        // Step 1：设置验证规则和错误消息
        rules: {
            _username: "required",
            _password: "required"
        },
        messages: {
            _username: "必须填写用户名。",
            _password: "必须填写密码。"
        },
        // Step 2：设置验证触发事件
        onfocusout: false,
        onkeyup: false,
        focusInvalid: false,
        // Step 3：设置错误信息提示
        wrapper: "li"
    });
});

document.onkeyup = function (e) {
    let _key = 0;
    if (e === null) {
        _key = e.keyCode;
    } else {
        _key = e.which;
    }

    if (_key === 13) {
        $("#but_signup").focus();
        $(".note").text("");
        $(".note").removeClass("note__up").addClass("note__down");
        $('#but_signup').click();
    }
};